<?php

namespace addons\cms\controller\wxapp;


use app\admin\model\ShopOrder;
use app\admin\model\ShopProduct;
use app\common\model\ScoreLog;
use app\common\model\User;
use think\Db;
use const true;
use function cdnurl;
use function date;
use function explode;
use function rand;
use function time;

/**
 * 积分商城
 */
class Shop extends Base
{
    protected $noNeedLogin = ['productList', 'productDetail'];

    protected $token = '';

    /*获取商品列表*/
    public function productList()
    {
        $where['saleswitch'] = 1;//已上架的商品
        $model = new ShopProduct();
        $list = $model->where($where)
            ->order( 'id DESC')
            ->select();
        foreach ($list as &$row) {
            $images = explode(',', $row['images']);
            foreach($images as &$v){
                $v = cdnurl($v, true);
            }
            $row['images'] = $images;
        }
        $this->success( '', $list);
    }

    public function productDetail()
    {
        $id = (int)$this->request->post('id');//商品id
        if (!$id ) $this->error(__('Operation failed'));
        $product = ShopProduct::get($id);
        if (!$product) {
            $this->error(__('商品不存在'));
        }
        //$product->content = str_replace('src="', 'src="' . cdnurl('', true), $product->content);
        $this->success('', $product);
    }

    /*兑换商品*/
    public function orderProduct()
    {
        $id = (int)$this->request->post('id');//商品id
        $product = ShopProduct::get($id);
        if (!$product) {
            $this->error(__('商品不存在'));
        }else{
            if($product->stock <= 0) $this->error(__('商品库存不足'));
        }
        if(1*$this->auth->score<1*$product->integral) $this->error(__('当前积分不足'));
        //积分进行兑换
        Db::startTrans();
        //生成兑换订单
        $shopOrderModel = new ShopOrder();
        $shopOrderModel->order_no               = date('YmdHis').rand(10000,99999);//订单号
        $shopOrderModel->shop_product_id        = $product->id;//商品id
        $shopOrderModel->user_id                = $this->auth->id;//兑换用户id
        $shopOrderModel->createtime             = time();
        $shopOrderModel->integral               = $product->integral;//兑换积分
        $shopOrderModel->code                   = date('YmdHis').rand(10000,99999);//兑换码
        $shopOrderModel->status                 = 0;//默认未兑换状态
        $orderResult = $shopOrderModel->save();
        $id = $shopOrderModel->id;
        //扣除积分
        User::score(-1*$product->integral, $this->auth->id, '商品兑换');
        if($orderResult !==false){
            Db::commit();
        }else{
            Db::rollback();
            $this->error(__('兑换失败'));
        }
        $this->success('', ['id'=>$id]);
    }
    /*积分兑换订单*/
    public function orderDetail()
    {
        $id = (int)$this->request->post('id');//商品id
        if (!$id ) $this->error(__('Operation failed'));
        $orderModel = new ShopOrder();
        $order = $orderModel->with(['shopproduct'=>['shopstore']])->find(['shop_order.id'=>$id, 'shop_order.user_id'=>$this->auth->id]);
        if (!$order) {
            $this->error(__('订单不存在'));
        }
        $order->shopproduct->shopstore;
        $this->success('', $order);
    }

    /*积分兑换订单*/
    public function orderList()
    {
        $status = $this->request->post('status');//获取订单状态筛选 0=待兑换 1=已兑换 -1为全部
        $where['user_id'] = $this->auth->id;//已上架的商品
        if($status!=-1) $where['status'] = $status;
        $model = new ShopOrder();
        $list = $model->where($where)
            ->with(['shopproduct'=>['shopstore']])
            ->order( 'id DESC')
            ->select();
        foreach ($list as &$row) {
            if($row['status']==0){
                $row['status_zh'] = "待兑换";
            }else if($row['status']==1){
                $row['status_zh'] = "已兑换";
            }
            $row['createtime_text'] = date("Y-m-d H:i:s", $row['createtime']);
            $row->shopproduct->shopstore;
        }
        $this->success( '', $list);
    }

    /*用户积分明细列表*/
    public function scoreList()
    {
        $status = $this->request->post('status');//获取订单状态筛选 暂定0为全部 尚有下拉框筛选没做
        $where['user_id'] = $this->auth->id;//已上架的商品
//        if($status!=0) $where['status'] = $status;
        if($status==1) $where['score'] = ['<', 0];
        if($status==2) $where['score'] = ['>', 0];
        $model = new ScoreLog();
        $list = $model->where($where)
            ->order( 'id DESC')
            ->select();
        foreach ($list as &$row) {
            $row['createtime_text'] = date("Y-m-d H:i:s", $row['createtime']);
        }
        $this->success( '', $list);
    }
}
